/*
 * AppInfo.java
 * 
 * Copyright©2012-2013 JMAS corporation. All rights reserved.
 */
package jp.co.jmas.jmasvideo;

import java.io.File;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Application;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.media.CamcorderProfile;
import android.os.Environment;
import android.preference.PreferenceManager;

public class AppInfo extends Application {
	/** Singleton */
	private static AppInfo instance;

	/** Url of AppEngine */
	private static final String SERVER_APPENGINE = "https://1.jmasmatamura.appspot.com";

	/** Bucket name */
	public static final String BUCKET = "jmas_camera";

	public AppInfo() {
		synchronized (AppInfo.class) {
			if (instance == null) {
				instance = this;
			}
		}
	}

	/**
	 * returns video directory
	 * @return
	 */
	public static File getVideoDir() {
		return instance.getExternalFilesDir(Environment.DIRECTORY_MOVIES); // アプリ固有
	}

	/**
	 * returns thumbnail directory (directory)
	 * @return
	 */
	public static File getThumbnailDir() {
		return instance.getExternalFilesDir(Environment.DIRECTORY_PICTURES); // アプリ固有
	}

	/**
	 * returns the file list in the video directory
	 * @return
	 */
	public static File[] getVideoFiles() {
		return getVideoDir().listFiles();
	}

	/**
	 * returns accounts of google registered in the phone
	 * @return
	 */
	public static Account[] getAccounts() {
		AccountManager manager = AccountManager.get(instance);
		return manager.getAccountsByType("com.google");
	}

	/**
	 * @return
	 * @deprecated
	 */
	public static int getVideoQuality() {
		// 現在使用ユーザアカウントの取得
		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(instance.getApplicationContext());
		String quality = pref.getString(SettingActivity.KEY_QUALITY, "LOW");

		if ("HIGH".equals(quality)) {
			return CamcorderProfile.QUALITY_HIGH;
		}
		return CamcorderProfile.QUALITY_LOW;
	}

	public static String getServer() {
		// 現在使用ユーザアカウントの取得
		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(instance.getApplicationContext());
		String server = pref.getString(SettingActivity.KEY_SERVER, null);
		if (server == null) {
			server = SERVER_APPENGINE;
			Editor editor = pref.edit();
			editor.putString(SettingActivity.KEY_SERVER, server);
			editor.commit();
		}
		return server;
	}

	/**
	 * 最大録画時間
	 * @return
	 */
	public static int getMaxDuration() {
		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(instance.getApplicationContext());
		int maxDuration = pref.getInt(SettingActivity.KEY_MAX_DURATION, 30000);
		return maxDuration;
	}
}
